package com.zhuss.order.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.CancelOrderBo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.entity.Order;
import com.zhuss.order.processor.unuse.OrderUnuse;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.util.JsonUtil;
/**
 * 
 * @author zhushanshuo
 * @since 2020.03.09 13:10
 *
 */

@Component("orderCancel")
public class OrderCancel extends AbstractProcessor<Object, CancelOrderBo> implements Processor<Object, CancelOrderBo> {
	
	@Autowired
	private TrainDao trainDao;
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderItemDao orderItemDao;
	
	@Autowired
	private OrderCheck orderCheck;
	
	@Autowired
	private OrderRead orderRead;
	
	@Override
	public void check(CancelOrderBo bo) throws DzcxException {
		Long orderId = bo.getOrderId();
		Long userId = bo.getConcurrentUserId();
		if (orderId == null || orderId <= 0) {
			throw new DzcxException(ExceptionCode.订单ID为空);
		}
		DetailOrderVo vo = (DetailOrderVo) orderRead.detail(orderId);
		if (vo == null || vo.getOrderId() == null) {
			throw new DzcxException(ExceptionCode.订单ID不存在);
		}
		bo.setOrder(vo);
		// 当前订单不满足取消条件
		if (!orderCheck.checkCancel(vo)) {
			throw new DzcxException(ExceptionCode.当前订单不满足取消条件);
		}
		
		// 每个用户只能取消自己的订单
		if (!orderCheck.checkBelongUser(vo, userId)) {
			throw new DzcxException(ExceptionCode.每个用户只能取消自己的订单);
		}
	}
	
	@Override
	public Object process(CancelOrderBo bo) throws DzcxException {
		Long userId = bo.getConcurrentUserId();
		String name = bo.getConcurrentUserName();
		Date now = new Date();
		Long orderId = bo.getOrderId();
		DetailOrderVo vo = bo.getOrder();
		
		OrderUnuse.startUnuseDecrUsed(vo, orderCheck, orderDao, orderItemDao, trainDao, name, userId, OrderStatus.未支付之前取消.key());
		
		Order order = new Order();
		order.setOrderId(orderId);
		order.setModifierId(userId);
		order.setModifier(name);
		order.setModifyTime(now);
		order.setStatus(OrderStatus.未支付之前取消.key());
		orderDao.updateByPrimaryKeySelective(order);
		
		orderItemDao.cancel(orderId, OrderStatus.未支付之前取消.key(), order.getModifier(), order.getModifierId(), order.getModifyTime());
		return null;
	}

	@Override
	public Object log(CancelOrderBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.取消订单.key());
    	record.setTargetId(param.getOrderId());
    	record.setTargetName(param.getOrderId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}


}
